﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Tomato.Text
{
    public class ValueUtils
    {

        /// <summary>
        /// 产生任意长度的随机数。
        /// </summary>
        /// <param name="length">任意长度</param>
        /// <returns></returns>
        public static string GenerateRandomString(int length)
        {
            System.Text.StringBuilder MyStringBuilder = new System.Text.StringBuilder();
            char[] chars = "0123456789".ToCharArray();
            int index;
            Random rnd = new Random();
            for (int i = 0; i < length; i++)
            {
                index = rnd.Next(chars.Length);
                MyStringBuilder.Append(chars[index].ToString());
            }
            return MyStringBuilder.ToString();
        }



        /// <summary>
        /// 正浮点数
        /// </summary>
        /// <param name="strEmail">正浮点数价格</param>
        /// <returns>判断结果</returns>
        public static bool IsZhengfudianshu(string str)
        {
            //return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");//这个如果是2010-123-23这样的它也会匹配实际上程序中会出错。
            return new Regex(@"^\d*(?:$|\.\d*$)").IsMatch(str);
        }

        //是否是电话号码格式
        public static bool IsDianhuahaoma(string str)
        {
            return new Regex(@"[\d+-]+").IsMatch(str);
        }

        //是否是邮政编码
        public static bool IsYouzhengbianma(string str)
        {
            return new Regex(@"^\d{6}$").IsMatch(str);
        }

        //是否是网址
        public static bool IsWangzhi(string str)
        {
            return new Regex(@"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$").IsMatch(str);
        }




        /// <summary>
        /// 岗位年限
        /// </summary>
        /// <param name="strEmail"></param>
        /// <returns>判断结果</returns>
        public static bool IsGangweinianxian(string str)
        {
            //return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");//这个如果是2010-123-23这样的它也会匹配实际上程序中会出错。
            return new Regex(@"^[0-9]*$").IsMatch(str);
        }


        /// <summary>
        /// 验证非零的正整数：
        /// </summary>
        /// <param name="strEmail">验证非零的正整数：</param>
        /// <returns>判断结果</returns>
        public static bool IsZhengzhengshu(string str)
        {
            //return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");//这个如果是2010-123-23这样的它也会匹配实际上程序中会出错。
            return new Regex(@"^\+?[1-9][0-9]*$").IsMatch(str);
        }

        /// <summary>
        /// 检测是否符合yy-mm-dd格式
        /// </summary>
        /// <param name="strEmail">要判断的yy-mm-dd字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsYearmothday(string strYearmothday)
        {
            //return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");//这个如果是2010-123-23这样的它也会匹配实际上程序中会出错。
            return !Regex.IsMatch(strYearmothday, @"^\d{4}-(0?\d|1[012])-(0?\d|[12]\d|3[01])$");
        }

        /// <summary>
        /// 检测是否符合yy-mm-dd或者yy-m-d格式
        /// </summary>
        /// <param name="strEmail"></param>
        /// <returns>判断结果</returns>
        public static bool IsYearmothdaydan(string strYearmothday)
        {
            //return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");//这个如果是2010-123-23这样的它也会匹配实际上程序中会出错。
            //return !Regex.IsMatch(strYearmothday, @"[0-9-]*");
            //  return new Regex(@"[0-9-]*").IsMatch(strYearmothday);

            return new Regex(@"^[0-9-]+$").IsMatch(strYearmothday);//^[\\d\\.\\(\\)-]+$

        }


        /// <summary>
        /// 检测是否符合yy-mm-dd HH:MM:SS格式
        /// </summary>
        /// <param name="strEmail">要判断的yy-mm-dd HH:MM:SS字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsYearmothdayAndsfm(string strYearmothday)
        {
            // return Regex.IsMatch(strYearmothday, @"^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$");//这个如果是2010-12-23 123：00：00 它也会匹配 但是程序中会报错。
            return !Regex.IsMatch(strYearmothday, @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|(1[0-9])|(2[0-3]))\:(([0-5][0-9])|([0-9]))(((\s)|(\:(([0-5][0-9])|([0-9]))))?)))?$");
        }


        public static bool IsZimu(string strZimu)
        {
            return Regex.IsMatch(strZimu, @"^[a-zA-Z]+$");
        }

        public static bool Isteshuzifu(string strIn)
        {
            // Return true if strIn is in valid 
            Regex regExp = new Regex("[~!@#$%^&*()=+[\\]{}''\";/?.,><`|！·￥…—（）\\、；：。，》《]");//[~!@#$%^&*()=+[\\]{}''\";:/?.,><`|！·￥…—（）\\-、；：。，》《]
            return regExp.IsMatch(strIn);
        }
        /// <summary>
        /// 判断给定的字符串(strNumber)是否是数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumber(string strNumber)//
        {
            return new Regex(@"^\+?[1-9][0-9]*$").IsMatch(strNumber);//@"^([0-9])[0-9]*(\.\w*)?$"
            /*
            Regex objNotNumberPattern=new Regex("[^0-9.-]");
            Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
            Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
            string strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
            string strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
            Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
            return !objNotNumberPattern.IsMatch(strNumber) &&
                !objTwoDotPattern.IsMatch(strNumber) &&
                !objTwoMinusPattern.IsMatch(strNumber) &&
                objNumberPattern.IsMatch(strNumber);
            */
        }

        public static int getRandomNum(int min, int max)
        {
            Random ro = new Random();
            int iResult;
            iResult = ro.Next(min, max);
            return iResult;
        }

        /// <summary>
        /// 判断给定的字符串数组(strNumber)中的数据是不是都为数值型
        /// </summary>
        /// <param name="strNumber">要确认的字符串数组</param>
        /// <returns>是则返加true 不是则返回 false</returns>
        public static bool IsNumberArray(string[] strNumber)
        {
            if (strNumber == null)
            {
                return false;
            }
            if (strNumber.Length < 1)
            {
                return false;
            }
            foreach (string id in strNumber)
            {
                if (!IsNumber(id))
                {
                    return false;
                }
            }
            return true;

        }
        /// <summary>
        /// 检测是否符合email格式
        /// </summary>
        /// <param name="strEmail">要判断的email字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsValidEmail(string strEmail)
        {
            return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
        /// <summary>
        /// 判断是否为base64字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsBase64String(string str)
        {
            //A-Z, a-z, 0-9, +, /, =
            return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
        }
        /// <summary>
        /// 检测是否有Sql危险字符
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果，true不存在威胁，false存在威胁</returns>
        public static bool IsSafeSqlString(string str)
        {
            string strKeyWordRegex = @"or|and|select|xp_cmdshell|exec master|netlocalgroup|select|insert|delete|from|count|drop|table|update|truncate";
            string strSignRegex = @"[;|,|%|@|*|!|']";
            if (Regex.IsMatch(str, strSignRegex) || Regex.IsMatch(str, strKeyWordRegex, RegexOptions.IgnoreCase))
                return false;
            return true;

        }

        /// <summary>
        /// 检测是否有危险的可能用于链接的字符串
        /// </summary>
        /// <param name="str">要判断字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsSafeUserInfoString(string str)
        {
            return !Regex.IsMatch(str, @"/^\s*$|^c:\\con\\con$|[%,\*" + "\"" + @"\s\t\<\>\&]|$guestexp/is");
        }

        public static bool IsNullorEmpty(string text)
        {
            return text == null || text.Trim() == string.Empty;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsTime(string timeval)
        {
            return Regex.IsMatch(timeval, @"^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
        }
        /// <summary>
        /// 判断文件名是否为浏览器可以直接显示的图片文件名
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns>是否可以直接显示</returns>
        public static bool IsImgFilename(string filename)
        {
            filename = filename.Trim();
            if (filename.EndsWith(".") || filename.IndexOf(".") == -1)
            {
                return false;
            }
            string extname = filename.Substring(filename.LastIndexOf(".") + 1).ToLower();
            return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
        }

        /// <summary>
        /// 是否为ip
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
    }
}
